//
//  YHPopBaseView.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/9/23.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "YHPopBaseView.h"
#import "NSObject+Keyboard.h"
#import "UIColor+YHStyle.h"
#import "YHConstant.h"

@interface YHPopBaseView()

@property (retain, nonatomic, readwrite) UIView * backgroundView;
@property (assign, nonatomic, readwrite) BOOL isShowed;

@end

@implementation YHPopBaseView


-(void)yh_commonInit{
    
    self.tapBgToClose = YES;
    
    // 初始加载window上
    self.frame = [UIScreen mainScreen].bounds;
    [[UIApplication sharedApplication].keyWindow addSubview:self];

    self.backgroundView = [UIView new];
    self.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    self.backgroundView.alpha = 0;
    [self addSubview:self.backgroundView];
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.contentView = [UIView new];
    self.contentView.backgroundColor = [UIColor yh_background];
    self.contentView.layer.cornerRadius = 10;
    self.contentView.layer.masksToBounds = YES;
    [self addSubview:self.contentView];
    
    [self yh_setupUI];
    
    [self yh_keyboardShowAtAnimationView:self.contentView];
}

- (void)popShow{
    
    if (self.superview) {
        [self removeFromSuperview];
    }
    
    UIView * superV = self.popSuperV;
    if(!superV){
        superV = [UIApplication sharedApplication].keyWindow;
    }
    self.frame = superV.bounds;
    [superV addSubview:self];
    
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superV);
    }];
    
    [self yh_setupData];
    
    self.backgroundView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundView.alpha = 1;
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.isShowed = YES;
        });
    }];
    
    [self yh_popShowAnimation];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yh_tapBgEvent)];
    [self.backgroundView addGestureRecognizer:tap];
    
    
    if(self.didShowBlock){
        self.didShowBlock();
    }
}

- (void)popDismiss{
    
    [self yh_popDismissAnimation];
    
    
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
        self.contentView.alpha = 0;
        self.closeBtn.alpha = 0;
    } completion:^(BOOL finished) {
        if(finished){
            [self removeFromSuperview];
        }
        if(self.didCloseBlock){
            self.didCloseBlock();
        }
    }];
   
}


-(void)addCloseBtn:(SEL)action{
    [self addCloseBtn:action closeImg:nil];
}

-(void)addCloseBlock:(void(^)(void))block{
    self.clickCloseBlock = block;
    [self addCloseBtn:@selector(clickCloseEvent) closeImg:nil];
}

- (void)clickCloseEvent{
    if(self.clickCloseBlock){
        self.clickCloseBlock();
    }
    [self popDismiss];
}

-(void)addCloseBtn:(SEL)action closeImg:(UIImage * _Nullable)closeImg
{
    [self creatCloseBtn:action closeImg:closeImg];
}
- (void)addCloseButton{
    [self addCloseBtn:@selector(popDismiss) closeImg:nil];
}

- (void)addCloseButtonWithCloseImg:(NSString *)closeStr{
    UIImage *closeImg = [UIImage imageNamed:closeStr];
    [self addCloseBtn:@selector(clickCloseEvent) closeImg:closeImg];
    
}

- (UIButton *)creatCloseBtn:(SEL)action
{
    return  [self creatCloseBtn:action closeImg:nil];
}


-(UIButton *)creatCloseBtn:(SEL)action closeImg:(UIImage *)closeImg{
    
    //关闭按钮
    UIButton * closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.closeBtn = closeBtn;
    
    closeBtn.adjustsImageWhenDisabled = NO;
    closeBtn.adjustsImageWhenHighlighted = NO;
    
    [closeBtn setImage:closeImg?closeImg:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    closeBtn.backgroundColor = [UIColor clearColor];
    [self addSubview:closeBtn];
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(Adapted(40), Adapted(40)));
    }];
    
    return closeBtn;
}

#pragma mark - sub
- (void)yh_setupUI{
    
}

- (void)yh_popShowAnimation{
    
    //设置毛玻璃效果
    if(self.isBlurShow){
        
        self.backgroundView.backgroundColor = [UIColor clearColor];
        
        self.contentView.backgroundColor = [UIColor yh_background];
        
        
        UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        UIVisualEffectView * effectV = [[UIVisualEffectView alloc] initWithEffect:blur];
        effectV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        effectV.alpha = 0.9;
        [self insertSubview:effectV atIndex:0];
        [effectV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    
    self.contentView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    self.closeBtn.transform = CGAffineTransformMakeScale(0.5, 0.5);
    double duration = 0.3;
    if (self.popFromBottom) {
        self.contentView.transform = CGAffineTransformMakeTranslation(0, 400);
        duration = 0.5;
    }
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.contentView.transform = CGAffineTransformIdentity;
        self.closeBtn.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)yh_popDismissAnimation{
    if (self.popFromBottom) {
        self.contentView.transform = CGAffineTransformIdentity;
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.contentView.transform = CGAffineTransformMakeTranslation(0, 400);
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)yh_setupData{
    
}

- (void)yh_tapBgEvent{
    if(self.tapBgToClose){
        [self popDismiss];
    }
}
//
///** 弹出的视图 添加到目标视图上 如果没有则添加到window上*/
//- (UIView *)popVAddToView{
//    return nil;
//}


+ (void)cleanPopView{
    for(UIView * subV in [UIApplication sharedApplication].keyWindow.subviews){
        if([subV isKindOfClass:[YHPopBaseView class]]){
            YHPopBaseView * popV = (YHPopBaseView *)subV;
            if(!popV.isAlwaysStay){
                [popV popDismiss];
            }
        }
    }
}



@end
